<?php

require_once '../application/modules/admin/controllers/SuperControllerAdmin.php';

class Admin_LabelsController extends Admin_SuperController {

    public function indexAction() {
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->layout->disableLayout();
            if ($this->_request->event == 'removeRow')
                $this->onRemoweRow($this->_request->id);
            if ($this->_request->event == 'updateCat')
                $this->onUpdateCat($this->getRequest()->getParams());
            if ($this->_request->event == 'createCat')
                $this->onCreateCat($this->getRequest()->getParams());
        }

        $this->view->order_field = isset($this->_request->order_field) ? $this->_request->order_field : 'order_by';
        $this->view->order_dir = isset($this->_request->order_dir) ? $this->_request->order_dir : 'ASC';

        $this->view->tableColumn = array(
            0 => array(
                'title' => 'title',
                'field' => 'title'
            ),
            1 => array(
                'title' => 'url',
                'field' => 'url'
            ),
            2 => array(
                'title' => 'description',
                'field' => 'description',
                'wrap'  => 'div150'
            ),
            3 => array(
                'title' => 'metakeywords',
                'field' => 'metakeywords',
                'wrap'  => 'div150'
            ),
            5 => array(
                'title' => 'order_by',
                'field' => 'order_by'
            ),
            6 => array(
                'title' => 'active',
                'field' => 'active'
            ),
            7 => array(
                'title' => 'is_web',
                'field' => 'is_web'
            ),
            8 => array(
                'title' => 'Change',
            ),
        );

        $view_page = new Zend_Session_Namespace('view_apage');
        $onPage = $view_page->on_page;
        //$onPage = 4;
        $page = (int) $this->_request->page;
        if (empty($page))
            $page = 1;

        $qAr = array(
            'table_name' => 'labels',
            'order_field' => $this->view->order_field,
            'title' => $this->_request->title_cat,
            'order_dir' => $this->view->order_dir,
            'offset' => ($page - 1) * $onPage,
            'limit' => $onPage,
        );
        $CONTM = new Admin_Model_Content();
        $this->view->info = $CONTM->getItems($qAr, $count_rows);
        $this->view->countRow = $count_rows; 

        $paginator = Zend_Paginator::factory((int) $this->view->countRow);
        $paginator->setDefaultItemCountPerPage($onPage);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;
        $this->view->onPage = $onPage;
    }

    protected $sructure = array(
        0 => array('name' => 'title', 'type' => 'str', 'valid' => true,),
        1 => array('name' => 'url', 'type' => 'str',),
        2 => array('name' => 'description', 'type' => 'text'),
        3 => array('name' => 'metakeywords', 'type' => 'text'),
        4 => array('name' => 'active','value'=>1, 'type' => 'one-checkbox',),
        5 => array('name' => 'is_web',  'noview'=>1, 'type' => 'one-checkbox',),
        6 => array('name' => 'order_by', 'type' => 'str', 'valid' => true, 'class' => ' thisInt', 'style' => 'width: 50px;'),
    );

    public function createLabelsAction() {
        Zend_Layout::getMvcInstance()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $this->info = array();
        $this->info['id'] = 0;
        $this->info['event'] = 'createCat';
        $this->info['fields'] = $this->sructure;
        include (APPLICATION_PATH . '/modules/admin/views/scripts/snippets/lego.phtml');
    }

    public function editLabelsAction() {
        Zend_Layout::getMvcInstance()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        $cat_id = (int) $this->_request->id;
        if (!empty($cat_id)) {
            $CONTM = new Admin_Model_Content();
            $info = $CONTM->getOneObj('labels', $cat_id);
            if (is_array($info)) {
                $this->info = array();
                $this->info['id'] = $info['id'];
                $this->info['event'] = 'updateCat';
                $this->info['fields'] = $this->addValue($this->sructure, $info);
                include (APPLICATION_PATH . '/modules/admin/views/scripts/snippets/lego.phtml');
            }
        } else {
            throw new Exception('no parameters required');
        }
    }

    protected function onRemoweRow($id) {
        if (!empty($id)) {
            $ACNTM = new Admin_Model_Content();
            $ACNTM->removeRow('labels', $id);
        }
    }
    
    protected function onUpdateCat($data) {
        if (!empty($data)) {
            if(empty ($data['url']))
                $data['url'] = strtolower (_translit (trim($data['title'])));
            $ACNTM = new Admin_Model_Content();
            $ACNTM->update('labels', $data);
        }
    }

    protected function onCreateCat($data) {
        if (!empty($data)) {
            
            $ACNTM = new Admin_Model_Content();
            if(empty ($data['url']))
                $data['url'] = 'tag-'.$ACNTM->createUrl ($data['title'], 'labels');
            $ACNTM->insert('labels', $data);
        }
    }

}
